/*
* Copyright (C) 2024 ~ 2025 Uniontech Technology Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "usbscansettingdlg.h"

#include "widgets/hframe1.h"
//#include <DFontSizeManager>
//#include <DTitlebar>
#include <QLabel>

#include <QBoxLayout>

USBScanSettingDlg::USBScanSettingDlg(QWidget *parent)
    : QFrame(parent)
//    , m_blurEffect(nullptr)
    , m_contentWidget(nullptr)
    , m_usbScanSettingItem(new ScheduleScanItem(this))
    , m_pFoundButtonGrop(new QButtonGroup(this))
    , m_askButton(nullptr)
    , m_autoButton(nullptr)
    , m_pCancelBtn(nullptr)
    , m_line(nullptr)
    , m_pConfirmBtn(nullptr)
    , m_usbScanSwitch(false)
    , m_usbScanType(0)
    , m_monitorInterFaceServer(nullptr)
{
    setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint);

    // 初始化安全中心gsetting配置
    m_gsetting = InvokerFactory::GetInstance().CreateSettings("com.deepin.dde.deepin-defender", QByteArray(), this);

    m_monitorInterFaceServer = InvokerFactory::GetInstance().CreateInvoker("com.deepin.defender.MonitorNetFlow",
                                                                           "/com/deepin/defender/MonitorNetFlow",
                                                                           "com.deepin.defender.MonitorNetFlow",
                                                                           ConnectType::SYSTEM, this);

    initUi();
    initData();
    initConnection();
}

void USBScanSettingDlg::initUi()
{
    // 设置为阻态
    setWindowModality(Qt::ApplicationModal);
    // 背景透明化
//    setAttribute(Qt::WA_TranslucentBackground);

    setFixedSize(512, 324);

//    // 背景透明化
//    m_blurEffect = new DBlurEffectWidget(this);
//    m_blurEffect->setBlendMode(DBlurEffectWidget::BehindWindowBlend);
//    // 设置模糊大小
//    m_blurEffect->resize(size());
//    m_blurEffect->lower();

    // 主布局
    QVBoxLayout *mainlayout = new QVBoxLayout(this);
    mainlayout->setContentsMargins(0, 0, 0, 0);
    mainlayout->setSpacing(0);
    mainlayout->setAlignment(Qt::AlignmentFlag::AlignTop);

    // 右上角关闭按钮
//    DTitlebar *titleBar = new DTitlebar(this);
//    titleBar->setAccessibleName("usbScanDialog_closeButton");
//    titleBar->setBackgroundTransparent(true);
//    titleBar->setTitle("");
//    titleBar->setFixedHeight(40);
//    titleBar->setMenuVisible(false);
//    QHBoxLayout *titleBarLayout = new QHBoxLayout();
//    titleBarLayout->setContentsMargins(0, 0, 0, 0);
//    titleBarLayout->addWidget(titleBar, 0, Qt::AlignRight | Qt::AlignTop);
//    mainlayout->addLayout(titleBarLayout);

    // 加入一个中间内容控件+布局  设置透明毛玻璃
//    DBlurEffectWidget *CenterWidget = new DBlurEffectWidget(this);
//    CenterWidget->setBlendMode(DBlurEffectWidget::BehindWindowBlend);
    QVBoxLayout *CenterLayout = new QVBoxLayout;
    CenterLayout->setContentsMargins(10, 10, 10, 10);
    CenterLayout->setSpacing(0);
//    CenterWidget->setLayout(CenterLayout);

    // 加入一个布局  将主要控制内容放置其中，按钮控制显示/隐藏
    m_contentWidget = new HFrame1(this);
    m_contentWidget->setBackgroundType(HFrame1::ItemBackground);
    m_contentWidget->setFixedSize(480, 120);

    // 标题 USB扫描
    QLabel *label_title = new QLabel(tr("USB Device Scan"), this);
    label_title->setAlignment(Qt::AlignCenter);
    QFont ft = label_title->font();
    ft.setBold(true);
    label_title->setFont(ft);
//    DFontSizeManager::instance()->bind(label_title, DFontSizeManager::T5);
    mainlayout->addWidget(label_title, 0, Qt::AlignHCenter | Qt::AlignTop);

    // 加入定时扫描设置选项
    m_usbScanSettingItem->setText(tr("Auto scan USB devices"));
    m_usbScanSettingItem->setSwitchStatus(false);
    m_usbScanSettingItem->setFixedWidth(460);
    CenterLayout->addWidget(m_usbScanSettingItem, 0, Qt::AlignCenter);

    // 单选按钮组
    QVBoxLayout *fLayout = new QVBoxLayout();
    fLayout->setContentsMargins(20, 0, 0, 0);
    fLayout->setSpacing(0);
    m_pFoundButtonGrop->setExclusive(true);

    // 加入发现病毒处理方式名称
    fLayout->addSpacing(20);
    QLabel *lbFoundName = new QLabel(this);
    lbFoundName->setText(tr("When viruses are found"));
    lbFoundName->setContentsMargins(0, 0, 0, 0);
    lbFoundName->setFont(ft);
//    DFontSizeManager::instance()->bind(lbFoundName, DFontSizeManager::T7);
    fLayout->addWidget(lbFoundName, 0, Qt::AlignLeft);

    // 询问按钮
    m_askButton = new QRadioButton(this);
    m_askButton->setText(tr("Ask me"));
//    DFontSizeManager::instance()->bind(m_askButton, DFontSizeManager::T7);
    fLayout->addSpacing(5);
    fLayout->addWidget(m_askButton);
    m_pFoundButtonGrop->addButton(m_askButton);

    // 自动处理按钮
    m_autoButton = new QRadioButton(this);
    m_autoButton->setText(tr("Auto remove"));
//    DFontSizeManager::instance()->bind(m_autoButton, DFontSizeManager::T7);
    fLayout->addSpacing(5);
    fLayout->addWidget(m_autoButton);
    fLayout->addStretch(1);
    m_pFoundButtonGrop->addButton(m_autoButton);
    m_contentWidget->setLayout(fLayout);

    /****************取消按钮及确认按钮设置***************/

    // 取消按钮
    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->setAlignment(Qt::AlignHCenter);

    m_pCancelBtn = new QPushButton(this);
    m_pCancelBtn->setText(tr("Cancel", "button"));
    m_pCancelBtn->setAccessibleName("usbScanDialog_cancelButton");
    m_pCancelBtn->setMinimumSize(QSize(PB_CANCLE_WIDTH, PB_CANCLE_HEIGHT));
    m_pCancelBtn->setMaximumSize(QSize(PB_CANCLE_WIDTH, PB_CANCLE_HEIGHT));
    connect(m_pCancelBtn, SIGNAL(clicked()), this, SLOT(close()));

    btnLayout->addWidget(m_pCancelBtn);
    btnLayout->addSpacing(8);

    // 分割线
    m_line = new QFrame(this);
    m_line->setMaximumHeight(LINE_HEIGHT);
    m_line->setFrameShape(QFrame::VLine);

    btnLayout->addWidget(m_line);
    btnLayout->addSpacing(8);

    // 确认按钮
    m_pConfirmBtn = new QPushButton(this);
    m_pConfirmBtn->setText(tr("Confirm", "button"));
    m_pConfirmBtn->setAccessibleName("usbScanDialog_confirmButton");
    m_pConfirmBtn->setMinimumSize(QSize(PB_CANCLE_WIDTH, PB_CANCLE_HEIGHT));
    m_pConfirmBtn->setMaximumSize(QSize(PB_CANCLE_WIDTH, PB_CANCLE_HEIGHT));
    m_pConfirmBtn->setAttribute(Qt::WA_NoMousePropagation);
    btnLayout->addWidget(m_pConfirmBtn);

    // 将中心控件放入整体布局
    CenterLayout->addWidget(m_contentWidget);
    CenterLayout->addSpacing(20);
    CenterLayout->addLayout(btnLayout);
//    mainlayout->addWidget(CenterWidget);
    mainlayout->addLayout(CenterLayout);
}

void USBScanSettingDlg::initData()
{
    m_usbScanSwitch = m_gsetting->GetValue(AUTO_SCAN_USB).toBool();
    setWidgetStatus(m_usbScanSwitch);
    m_usbScanSettingItem->setSwitchStatus(m_usbScanSwitch);
    m_usbScanType = m_gsetting->GetValue(USB_STORAGE_DEVICES_TYPE).toInt();
    if (m_usbScanType == 0) {
        m_askButton->setChecked(true);
    } else {
        m_autoButton->setChecked(true);
    }
}

void USBScanSettingDlg::initConnection()
{
    connect(m_usbScanSettingItem, SIGNAL(switchTrigger(bool)), this, SLOT(acceptUSBScanSwitchStatusChange()));
    connect(m_pConfirmBtn, SIGNAL(clicked()), this, SLOT(acceptConfirmButtonClicked()));
}

void USBScanSettingDlg::acceptConfirmButtonClicked()
{
    if (m_gsetting) {
        if (m_gsetting->GetValue(AUTO_SCAN_USB).toBool() != m_usbScanSettingItem->getSwitchStatus()) {
            if (m_usbScanSettingItem->getSwitchStatus()) {
                m_gsetting->SetValue(AUTO_SCAN_USB, true);
                DBUS_NOBLOCK_INVOKE(m_monitorInterFaceServer, "AddSecurityLog", SECURITY_LOG_TYPE_ANTIAV, tr("Scan USB storage devices: ON"));
            } else {
                m_gsetting->SetValue(AUTO_SCAN_USB, false);
                DBUS_NOBLOCK_INVOKE(m_monitorInterFaceServer, "AddSecurityLog", SECURITY_LOG_TYPE_ANTIAV, tr("Scan USB storage devices: OFF"));
            }
        }

        bool isChanged = (m_askButton->isChecked() && (m_usbScanType == 0))
                         || (!m_askButton->isChecked() && (m_usbScanType == 1));
        if (!isChanged) {
            if (m_askButton->isChecked()) {
                m_gsetting->SetValue(USB_STORAGE_DEVICES_TYPE, 0);
                DBUS_NOBLOCK_INVOKE(m_monitorInterFaceServer, "AddSecurityLog", SECURITY_LOG_TYPE_ANTIAV, tr("Changed security settings of USB storage devices to \"Ask me\""));
            } else if (m_autoButton->isChecked()) {
                m_gsetting->SetValue(USB_STORAGE_DEVICES_TYPE, 1);
                DBUS_NOBLOCK_INVOKE(m_monitorInterFaceServer, "AddSecurityLog", SECURITY_LOG_TYPE_ANTIAV, tr("Changed security settings of USB storage devices to \"Auto remove threats\""));
            }
        }
    }

    this->close();
}

void USBScanSettingDlg::setWidgetStatus(bool isEnabled)
{
    m_contentWidget->setEnabled(isEnabled);
}

void USBScanSettingDlg::acceptUSBScanSwitchStatusChange()
{
    setWidgetStatus(m_usbScanSettingItem->getSwitchStatus());
}
